// pages/buy/numItem/numItem.js
Component({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0
  },

  properties: {
    range: Array
  },

  pageLifetimes: {
    show: function() {
      this.initNum()
    }
  },

  methods: {
    onTap: function(e) {
      const {range} = this.data
      let count = wx.getStorageSync('count')
      const limit = wx.getStorageSync('limit')
      const { currentTarget: { dataset: {type} } } = e
      const max = range[1] ? range[1] : 9999
      const min = range[0] ? range[0] : 0

      console.log(type)

      if(type === 'add') {
        count++
        console.log(count)

        if (count > max) {
          count = max
        }
      }else {
        count--

        if (count < limit) {
          count = limit
        }
      }

     wx.setStorageSync('count', count)

      // console.log(count)
      this.setData({
        num: count
      })

      this.triggerEvent('change',{count})
    },
    initNum: function () {
      const { range } = this.data
      const min = range[0] ? range[0] : 0
      const limit = wx.getStorageSync('limit')

      console.log(range, 'render range');

      this.setData({
        num: limit
      }, () => {
        wx.setStorageSync('count', limit)
      })
    }
  },

  ready: function() {
    this.initNum()
  }
  
})